package com.hy.common.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hy.common.consts.CommonConst;
import com.hy.common.utils.CommonUtils;
import com.hy.common.utils.MyObjectUtils;
import com.hy.entity.pojo.HyPlace;
import com.hy.entity.pojo.HyPropertySort;
import com.hy.entity.vo.HyPlaceReadVO;
import com.hy.entity.vo.HyPropertySortReadVO;
import com.hy.mapper.HyDepartmentMapper;
import com.hy.mapper.HyPlaceMapper;
import com.hy.mapper.HyPropertySortMapper;
import com.hy.mapper.HyUnitMapper;
import com.hy.service.HyPlaceService;
import com.hy.service.HyPropertySortService;
import org.springframework.util.ObjectUtils;

import java.util.*;

public class HyPropertySortImport extends AbstractExcelImport<HyPropertySortReadVO, HyPropertySort> {

    private HyPropertySortService sortService;
    private Integer runTimes=0;
    private HyUnitMapper unitMapper;
    private HyPropertySortMapper sortMapper;


    public HyPropertySortImport(HyPropertySortService sortService, HyUnitMapper unitMapper, HyPropertySortMapper sortMapper) {
        this.sortService = sortService;
        this.unitMapper = unitMapper;
        this.sortMapper = sortMapper;
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        boolean valid = validateHeader(headMap);
        if (!valid) {

            throw new RuntimeException("表头校验失败，请检查Excel文件的列名");
        }
    }
    private boolean validateHeader(Map<Integer, String> headMap) {

        Set<String> expectedHeaders=new HashSet<>();
        Collections.addAll(expectedHeaders,"单位名称","上级分类名","类别名称","国标代码");
        Set<String> actualHeaders = new HashSet<>(headMap.values());
        return actualHeaders.containsAll(expectedHeaders);
        // 检查表头是否与预期的列名一致
    }
    @Override
    public void invoke(HyPropertySortReadVO object, AnalysisContext context) {
        try{

            if (MyObjectUtils.areAllFieldsNull(object)){
                return;
            }
            HyPropertySort place=new HyPropertySort();

            Integer unitId = unitMapper.findUnitIdByUnitName(object.getUnitName());
            HyPropertySort hyPropertySort = sortMapper.selectOne(new LambdaQueryWrapper<HyPropertySort>().and(wapper ->
                    wapper.eq(HyPropertySort::getSortName,object.getSortName())
                            .eq(HyPropertySort::getUnitId, unitId)

            ));
            if (!ObjectUtils.isEmpty(hyPropertySort)){
                throw new ExcelAnalysisException("类别名称重复 ：第："+(context.getCurrentRowNum()+1)+"行");
            }

            Integer mgrId="第一级".equals(object.getMgrName())?CommonConst.DEFAULT_ID:sortMapper.findSortIdByUnitIDAndSortName(unitId,object.getMgrName());
            place.setUnitId(unitId);
            place.setDel(1);
            place.setCreateDate(CommonUtils.getDate());
            place.setCode(object.getCode());
            place.setSortName(object.getSortName());
            place.setSortMgr(mgrId);
            if (mgrId.equals(0)){
                HyPropertySort sort = sortMapper.selectOne(new LambdaQueryWrapper<HyPropertySort>().and(wapper ->
                        wapper.eq(HyPropertySort::getSortName,object.getSortName())
                                .eq(HyPropertySort::getUnitId, unitId)
                )) ;
                if (sort!=null){
                    throw  new ExcelAnalysisException("第一级菜单跟数据库的有重复,第："+(context.getCurrentRowNum()+1)+"行");
                }else {
                    sortMapper.insert(place);

                }
                return;
            }
            dataList.add(place);

        }catch (Exception e){
            throw new ExcelAnalysisException(e.getMessage());
        }
    }

    @Override
    protected void doSaveData(List<HyPropertySort> data) {
        sortService.saveStuListWithBatch(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        doSaveData(dataList);
    }
}
